<template>
  <div class="bookkeeping_grid">
    <div class="bookkeeping_class">
      <div class="big_class_item" v-if="type === 'pay'">
        <div class="class_item" v-for="(item, index) in data" :key="'big'+index" @click="handleSelectedBigTypeIndex(index)">
          <div class="class_item_icon"  :class="{ 'selected_item_icon': selectedBigTypeIndex === index }">
            <i :class="['iconfont', item.icon]"></i>
            <div class="class_item_more" v-if="type === 'pay'">...</div>
          </div>
          <div class="class_item_title">{{ item.name }}</div>
        </div>
      </div>
      <div class="small_class">
        <div :class="[type === 'pay' ? 'small_class_item' : 'income_class_item']">
          <div class="class_item" 
            v-for="(item, index) in data?data[selectedBigTypeIndex].children: []"
            :key="'small' + index" 
            @click="handleSelectedSmallTypeIndex(index)"
          >
            <div class="class_item_icon" :class="{ 'selected_item_icon': selectedSmallTypeIndex === index }">
              <i :class="['iconfont', item.icon]"></i>
            </div>
            <div class="class_item_title">{{ item.name }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { classListProps } from '@/utils/interface'
import { defineComponent, onMounted, PropType, ref, watch } from 'vue'
export default defineComponent({
  props: {
    data: {
      type: Array as PropType<classListProps[]>
    },
    type: {
      type: String
    }
  },
  setup(props, context) {
    const selectedBigTypeIndex = ref(0)
    const selectedSmallTypeIndex = ref(-1)
    watch(() => props.data, () => {
      selectedBigTypeIndex.value = 0
    })
    const type = ref({
      icon: '',
      titleId: '',
    })
    const handleSelectedBigTypeIndex = (index: number) => {
      selectedBigTypeIndex.value = index
      selectedSmallTypeIndex.value = -1
      if(props.data) {
        const {icon, id} = props.data[selectedBigTypeIndex.value]
        type.value = {
          icon,
          titleId: id
        }
      }
      context.emit('type', type.value)
    }
    const handleSelectedSmallTypeIndex = (index: number) => {
      selectedSmallTypeIndex.value = index
      if(props.data) {
        const { id, icon } = props.data[selectedBigTypeIndex.value].children[selectedSmallTypeIndex.value]
        type.value.titleId = id
        type.value.icon = icon
      }
      context.emit('type', type.value)
    }
    onMounted(() => {
      handleSelectedBigTypeIndex(0)
    })
    return {
      selectedBigTypeIndex,
      selectedSmallTypeIndex,
      handleSelectedBigTypeIndex,
      handleSelectedSmallTypeIndex
    }
  }
})
</script>

<style scoped>
  .bookkeeping_grid {
    height: calc(100vh - 380px);
    overflow-y: auto;
    padding: 20px 10px;
  }
  .big_class_item {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
  }
  .small_class_item {
    display: flex;
    flex-wrap: wrap;
    background-color: #f2f2f2;
    border-radius: 8px;
    padding: 12px 8px;
  }
  .income_class_item {
    display: flex;
    flex-wrap: wrap;
  }
  .class_item {
    width: 20%;
  }
  .class_item_icon {
    position: relative;
    border-radius: 50%;
    width: 50px;
    height: 50px;
    font-size: 30px;
    line-height: 50px;
    color: #fff;
    background-color: #ccc;
    margin: 0 auto;
  }
  .selected_item_icon {
    background-color: rgba(249, 195, 194, 0.4);
  }
  .class_item_more {
    position: absolute;
    bottom: 4px;
    right: -2px;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    border: #fff 1px solid;
    background-color: rgb(249, 195, 194);
    font-size: 12px;
    line-height: 6px;
    color: white;
  }
  .class_item_title {
    font-size: 14px;
    padding: 4px 0 10px;
    width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
</style>